package com.minhua.jijiehao2.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.minhua.jijiehao2.R;
import com.minhua.jijiehao2.base.adapter.ViewHolder;
import com.minhua.jijiehao2.utils.AppUtils;

/**
 * @author moo
 * @date 2017/11/29.
 * @describe
 */

public class CommonTitleView extends RelativeLayout {
    private int stateBarHeight = AppUtils.getStateBarHeight(getContext());
    private ViewHolder mViewHolder;

    public CommonTitleView(Context context) {
        this(context, null);
    }

    public CommonTitleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonTitleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mViewHolder = ViewHolder.get(getContext(), null, this, R.layout.layout_common_title, 0, true);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CommonTitleView, defStyleAttr, 0);
        initView(typedArray);
        typedArray.recycle();
    }

    /**
     * 是否支持沉浸式，
     *
     * @param stateBarHeight 传入0，表示不支持 否则不传
     */
    public void setStateBarHeight(int stateBarHeight) {
        this.stateBarHeight = stateBarHeight;
    }

    private void initView(TypedArray typedArray) {
        if (stateBarHeight != 0) {
            //支持沉浸式
            RelativeLayout rlTitle = mViewHolder.getView(R.id.rlTitle);
            RelativeLayout.LayoutParams layoutParams = (LayoutParams) rlTitle.getLayoutParams();
            layoutParams.setMargins(0, stateBarHeight, 0, 0);
            rlTitle.setLayoutParams(layoutParams);
        }
        for (int i = 0; i < typedArray.getIndexCount(); i++) {
            int index = typedArray.getIndex(i);
            switch (index) {
                case R.styleable.CommonTitleView_backIcon:
                    int resourceId = typedArray.getResourceId(index, R.mipmap.back_aw);
                    ImageView ivBack = mViewHolder.getView(R.id.ivLeft);
                    ivBack.setVisibility(VISIBLE);
                    ivBack.setImageResource(resourceId);
                    setOnBackListener(R.id.ivLeft);
                    break;
                case R.styleable.CommonTitleView_title:
                    String title = typedArray.getString(index);
                    if (!TextUtils.isEmpty(title)) {
                        mViewHolder.setText(R.id.tvTitle, title);
                    }
                    break;
                case R.styleable.CommonTitleView_background_color:
                    int color = typedArray.getColor(index, getContext().getResources().getColor(R.color.mainRed));
                    setBackgroundColor(color);
                    break;

                case R.styleable.CommonTitleView_rightText:
                    String rightText = typedArray.getString(index);
                    if (!TextUtils.isEmpty(rightText)) {
                        mViewHolder.setText(R.id.tvRight, rightText);
                        mViewHolder.setVisible(R.id.tvRight, View.VISIBLE);
                    }
                    break;
                case R.styleable.CommonTitleView_rightIcon:
                    int rightImgID = typedArray.getResourceId(index, R.mipmap.add);
                    mViewHolder.setImageResource(R.id.ivRight, rightImgID);
                    break;
                case R.styleable.CommonTitleView_dividerHeight:
                    float dimension = typedArray.getDimension(index, 0);
                    View divider = mViewHolder.getView(R.id.divider);
                    RelativeLayout.LayoutParams layoutParams = (LayoutParams) divider.getLayoutParams();
                    layoutParams.height = (int) dimension;
                    divider.setLayoutParams(layoutParams);
                    break;
                default:
                    break;
            }
        }
    }

    private void setOnBackListener(int id) {
        mViewHolder.setOnClickListener(id, new OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = getContext();
                if (context instanceof Activity) {
                    ((Activity) context).onBackPressed();
                }
            }
        });
    }

    public void setTitle(String title) {
        mViewHolder.setText(R.id.tvTitle, title);
    }

    public ViewHolder getViewHolder() {
        return mViewHolder;
    }
}
